﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenComposite.Base.Collections;
using OpenComposite.HumanActivity.Contracts.Participants;
using OpenComposite.Repository.Properties;

namespace OpenComposite.EII.Repository
{
	[RepositoryItem(ItemType.Role, "role", "role_id")]
	public class Role : RepositoryItemBaseEx, IParticipant
	{
		#region Initialization
		/// <summary>
		/// Initializes a new instance of the <see cref="Role"/> class.
		/// </summary>
		public Role()
			: base("role_id", "role_nm", "role_desc", "role_ntext", Resources.sNewRole)
		{
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="Role"/> class.
		/// </summary>
		/// <param name="bCreateNew">if set to <c>true</c> [b create new].</param>
		public Role(bool bCreateNew)
			: this()
		{
			if ( bCreateNew )
				this.CreateNew();
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="Role"/> class.
		/// </summary>
		/// <param name="id">The ID.</param>
		public Role(int id)
			: this()
		{
			this.Load(id);
		}
		#endregion

		#region Public Properties
		/// <summary>
		/// Gets the image key.
		/// </summary>
		/// <value>The image key.</value>
		public override string ImageKey
		{
			get { return "png_users"; }
		}

		/// <summary>
		/// Gets the selected image key.
		/// </summary>
		/// <value>The selected image key.</value>
		public override string SelectedImageKey
		{
			get { return "png_users"; }
		}

		/// <summary>
		/// Gets the image.
		/// </summary>
		/// <value>The image.</value>
		public override System.Drawing.Image Image
		{
			get { return Resources.png_users; }
		}

		/// <summary>
		/// Gets the selected image.
		/// </summary>
		/// <value>The selected image.</value>
		public override System.Drawing.Image SelectedImage
		{
			get { return Resources.png_users; }
		}

		/// <summary>
		/// Gets the type of the repository item.
		/// </summary>
		/// <value>The type of the repository item.</value>
		public override ItemType RepositoryItemType
		{
			get { return ItemType.Role; }
		}

		/// <summary>
		/// Gets the name of the repository item type.
		/// </summary>
		/// <value>The name of the repository item type.</value>
		public override string RepositoryItemTypeName
		{
			get { return "Role"; }
		}

		/// <summary>
		/// Gets the repository item type description.
		/// </summary>
		/// <value>The repository item type description.</value>
		public override string RepositoryItemTypeDescription
		{
			get { return ""; }
		}

		/// <summary>
		/// Gets the name of the table.
		/// </summary>
		/// <value>The name of the table.</value>
		public override string TableName
		{
			get { return "t_role"; }
		}

		/// <summary>
		/// Gets the allowed child types.
		/// </summary>
		/// <value>The allowed child types.</value>
		public override List<ItemType> AllowedChildTypes
		{
			get { return new List<ItemType>(); }
		}


		/// <summary>
		/// Gets or sets the organisation ID.
		/// </summary>
		/// <value>The organisation ID.</value>
		public int OrganisationID
		{
			get { return getInt("org_id", -1); }
			set { setValue("OrganisationID", "org_id", value); }
		}

		/// <summary>
		/// Gets or sets the organisation.
		/// </summary>
		/// <value>The organisation.</value>
		public Organization Organisation
		{
			get { return Global.Data.RepositoryService.GetItem<Organization>(OrganisationID); }
			set
			{
				if ( value == null ) OrganisationID = -1;
				else OrganisationID = value.ID;
			}
		}

		/// <summary>
		/// Gets or sets the organisation unit ID.
		/// </summary>
		/// <value>The organisation unit ID.</value>
		public int OrganisationUnitID
		{
			get { return getInt("role_orgunit_id", -1); }
			set { setValue("OrganisationUnitID", "role_orgunit_id", value); }
		}

		/// <summary>
		/// Gets or sets the organizational unit.
		/// </summary>
		/// <value>The organizational unit.</value>
		public OrganizationalUnit OrganizationalUnit
		{
			get { return Global.Data.RepositoryService.GetItem<OrganizationalUnit>(OrganisationUnitID); }
			set
			{
				if ( value == null ) OrganisationUnitID = -1;
				else OrganisationUnitID = value.ID;
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance is unit specific.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is unit specific; otherwise, <c>false</c>.
		/// </value>
		public bool IsUnitSpecific
		{
			get { return base.getBool("role_isunitspecific", false); }
			set { base.setValue("IsUnitSpecific", "role_isunitspecific", value); }
		}
		#endregion

		#region Public Members
		/// <summary>
		/// Gets the users.
		/// </summary>
		/// <returns></returns>
		public BindingListEx<User> GetUsers()
		{
			return Global.Data.GetOrgUsersByRole(this.ID);
		}

		/// <summary>
		/// Gets the icon.
		/// </summary>
		/// <returns></returns>
		public override Icon GetIcon()
		{
			Bitmap bmp = new Bitmap(this.Image);
			bmp.MakeTransparent(Color.Magenta);
			return Icon.FromHandle(bmp.GetHicon());
		}

		/// <summary>
		/// Shows the new item form.
		/// </summary>
		/// <param name="idRepository">The repository ID.</param>
		/// <param name="bAllowContinue">if set to <c>true</c> to allow save and continue.</param>
		/// <param name="parentItem">The parent item.</param>
		/// <param name="idDomain">The domain ID.</param>
		/// <param name="parentItemNew">The new parent item.</param>
		/// <returns></returns>
		public override DialogResult ShowNewItemForm(int idRepository, bool bAllowContinue, IRepositoryItem parentItem, ref int idDomain, out IRepositoryItem parentItemNew)
		{
			return base.ShowNewItemForm(idRepository, bAllowContinue, parentItem,
				ref idDomain, out parentItemNew, false, false, false);
		}

		/// <summary>
		/// Copies this instance.
		/// </summary>
		/// <param name="deep"><c>true</c> to recursively copy the item and all subitems;
		/// <c>false</c> to copy only the item itself.</param>
		/// <returns>The copied item.</returns>
		public override IRepositoryItem Copy(bool deep)
		{
			Role copy = new Role(true);
			copy.InitializeServiceProvider(this);
			this.CopyDataRowToTarget(copy);
			return copy;
		}

		#endregion


		#region IParticipant Members

		/// <summary>
		/// Gets the type of the participant.
		/// </summary>
		/// <value>The type of the participant.</value>
		public ParticipantType ParticipantType
		{
			get { return ParticipantType.Role; }
		}

		#endregion

		#region IEquatable<IParticipant> Members

		/// <summary>
		/// Indicates whether the current object is equal to another object of the same type.
		/// </summary>
		/// <param name="other">An object to compare with this object.</param>
		/// <returns>
		/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
		/// </returns>
		public bool Equals(IParticipant other)
		{
			return other != null && other.ParticipantType == this.ParticipantType && other.ID == this.ID;
		}

		#endregion
	}
}
